<template>
	<view class="details">
		<!-- 图片 -->
		<view class="details-top">
			<view class="main-img" v-if="activityDetails!=null">
				<u--image :showLoading="true" :src="activityDetails.picUrl" width="100%" height="240px"></u--image>
			</view>
		</view>
		<!-- 主体 -->
		<view class="details-main" v-if="activityDetails!=null">
			<view class="main-top">{{activityDetails.title}}</view>
			<view class="main-content">
				<view class="main-text">
					<view class="text-left">发布时间：</view>
					<view class="text-right">{{activityDetails.creatTime}}</view>
				</view>
				<view class="main-text">
					<view class="text-left">活动状态：</view>
					<view v-if="activityDetails.state==0">
						<view class="text-right">未开始</view>
					</view>
					<view v-if="activityDetails.state==1">
						<view class="text-right">进行中</view>
					</view>
					<view v-if="activityDetails.state==2">
						<view class="text-right">已结束</view>
					</view>
				</view>
			</view>
			<!-- 活动内容 -->
			<view class="main-xia">
				<view class="xia-top">----------------- 活动内容说明 -----------------</view>
				<view class="xia-content">
					<u-parse :content="activityDetails.content"></u-parse>
				</view>
			</view>

		</view>
		<view v-if="activityRecord!==null">
			<view class="details-button" v-if="activityRecord.length ==0">
				<u-button style="height:64" type="primary" @click="join()" text="立即报名"></u-button>

			</view>
			<view class="details-button" v-if="activityRecord.length ==1">
				<u-button style="height:64" type="error" @click="cancel()" text="取消报名"></u-button>
			</view>
			<view class="details-button" v-if="state ==2">
				<u-button style="height:64" type="warning" @click="over()" text="活动已结束"></u-button>
			</view>
			<view class="details-button" v-if="state ==0">
				<u-button style="height:64" type="error" @click="nostart()" text="活动未开始"></u-button>
			</view>
		</view>
		<u-loading-page :loading="loading"></u-loading-page>
	</view>
</template>

<script>
	import api from '@/api/api.js';
	export default {
		data() {
			return {
				activityDetails: null,
				activityRecord: null,
				loading:false,
				list:null,
				src: 'https://cdn.uviewui.com/uview/album/1.jpg',
			}
		},
		onLoad(option) {
			console.log(option)
			this.id = option.id,
				this.state = option.state
			this.getRecordList()
		},
		onShow() {
			this.getDetailsList()
		},
		methods: {
			//获取活动详情
			getDetailsList() {
				api.getActivityDetails({
					id: this.id,
				}).then(res => {
					console.log(res)
					this.activityDetails = res.data.data
				})
			},
			//获取活动记录列表
			getRecordList() {
				let userInfo = uni.getStorageSync('userInfo')
				api.getAcyivityRecord({
					userId: userInfo.id,
					exerciseId: this.id
				}).then(res => {
					console.log(res)
					this.activityRecord = res.data.rows
					console.log(this.activityRecord)
					if(res.data.code==200){
						this.loading =false
						console.log(res.data.code)
						console.log('报名成功')
						
					}else if(res.data.code==601){
						this.loading =false
						console.log(res.data.code)
						console.log('取消报名')
					}
				})
			},
			//立即报名
			join() {
				let userInfo = uni.getStorageSync('userInfo')
				api.getJoinAcyivity({
					userId: userInfo.id,
					exerciseId: this.id
				}).then(res => {
					if (res.data.code == 200) {
						uni.$u.toast('报名成功')
						this.getRecordList()//获取按钮颜色变化
						this.loading =true

					} else {
						uni.$u.toast('报名失败')
					}
				})
			},
			//取消报名
			cancel() {
				let id = this.activityRecord[0].id
				api.getCancelAcyivity(
					id
				).then(res => {
					if (res.data.code == 200) {
						uni.$u.toast('取消成功')
						this.getRecordList()
						this.loading =true
					} else {
						uni.$u.toast('取消失败')
					}
				})
			},
			//活动未开始
			nostart() {
				uni.$u.toast('活动未开始')
			},
			//活动结束
			over() {
				uni.$u.toast('活动已结束')
			},
		}
	}
</script>

<style lang="scss" scoped>
	.details {
		min-height: 100vh;
		margin: 0;
		background-color: #FFFFFF;
	}

	.details-top {
		background-color: #FFFFFF;
		width: 100%;
		border-radius: 20rpx;
		margin-bottom: 10rpx;
	}

	.details-main {
		padding: 32rpx;
		width: calc(100%-64rpx);
		height: 100%;
		background-color: #fff;
		border-top-left-radius: 80rpx;
		border-top-right-radius: 80rpx;
		position: relative;
		top: -80rpx;
	}

	.main-top {
		padding: 16rpx;
		width: calc(100%-32rpx);
		text-align: center;
		font-size: 36rpx;

	}

	.main-content {
		margin-bottom: 30rpx;
	}

	.main-text {
		padding: 10rpx 30rpx;
		display: flex;

		.text-left {
			font-size: 26rpx;
			color: #838585;

		}

		.text-right {
			font-size: 28rpx;
		}
	}

	.main-xia {
		padding: 16rpx 16rpx;
		width: calc(100%-32rpx);

		.xia-top {
			font-size: 24rpx;
			color: #838585;
			text-align: center;
			margin-bottom: 30rpx;
		}

		.xia-content {
			font-size: 28rpx;
			color: #838585;
		}

	}

	.details-button {
		position: fixed;
		width: calc(100% - 63rpx);
		padding: 32rpx;
		bottom: 0;
		left: 0;
		background-color: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: center;
		box-shadow: 0rpx 1rpx 8rpx 0rpx #e5e5e5;
	}
</style>
